package com.zixi.beverage_order.controller.seller;

import com.zixi.beverage_order.mapper.ReviewMapper;
import com.zixi.beverage_order.pojo.Product;
import com.zixi.beverage_order.pojo.ReturnProductInfo;
import com.zixi.beverage_order.pojo.Review;
import com.zixi.beverage_order.service.seller.ProductService;
import com.zixi.beverage_order.util.OSSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manage")
public class ProductManageController {
    @Autowired
    ProductService productService;

    @Autowired
    OSSClientUtil ossClientUtil;

    @Autowired
    ReviewMapper reviewMapper;

    @GetMapping("/all")
    public String getAllProduct(Model model) {
        List<ReturnProductInfo> returnProductInfoList = productService.getAllProduct();
        model.addAttribute("AllData", returnProductInfoList);
        return "topmenu/productmanage";
    }

    @GetMapping("/coffee")
    public String getCoffeeProduct(Model model) {
        List<ReturnProductInfo> returnProductInfoList = productService.getAllProduct();
        model.addAttribute("AllData", returnProductInfoList);
        return "productmanage/coffeeproduct.html";
    }

    @GetMapping("/mtea")
    public String getMteaProduct(Model model) {
        List<ReturnProductInfo> returnProductInfoList = productService.getAllProduct();
        model.addAttribute("AllData", returnProductInfoList);
        return "productmanage/mteaproduct.html";
    }

    @GetMapping("/newproduct")
    public String newProduct() {
        return "productmanage/newproduct";
    }

    @PostMapping("/insertproduct")
    public String insertProduct(@RequestParam("name") String name,
                                @RequestParam("category") Integer category,
                                @RequestParam("remark") String remark,
                                @RequestParam("price") String price,
                                @RequestParam("img") MultipartFile img) {

        Product product = new Product();
        product.setName(name);
        product.setCategory(category);
        product.setRemark(remark);
        product.setPrice(Integer.valueOf(price));
        String imgdata = ossClientUtil.checkImage(img);
        String[] picdata = imgdata.split("\\?");
        product.setPicdata(picdata[0]);
        productService.insertProduct(product);
        return "redirect:/manage/all";
    }

    @GetMapping("edit")
    public String editProduct(@RequestParam("id") String strid, Model model) {
        System.out.println(strid);
        Long id = Long.valueOf(strid);
        model.addAttribute("Product", productService.selectById(id));
        return "productmanage/productedit";
    }

    @GetMapping("delete")
    public String deleteProduct(@RequestParam("id") String strid) {
        Long id = Long.valueOf(strid);
        productService.deleteById(id);
//        return strid;
        return "redirect:/manage/all";
    }

    @PostMapping("update")
    public String updateProduct(@RequestParam("id") String strid,
                                @RequestParam("name") String name,
                                @RequestParam("category") Integer category,
                                @RequestParam("remark") String remark,
                                @RequestParam("price") String price,
                                @RequestParam("img") MultipartFile img
    ) {
        Product product;
        if (img.isEmpty()) {
            product = new Product(Long.parseLong(strid), name, category, remark, false, Integer.valueOf(price), null);
        } else {
            String imgdata = ossClientUtil.checkImage(img);
            String[] picdata = imgdata.split("\\?");
            product = new Product(Long.parseLong(strid), name, category, remark, false, Integer.valueOf(price), picdata[0]);
        }

        productService.updateProduct(product);
        return "redirect:/manage/all";
    }

    @GetMapping("/getList")
    public String getList(@RequestParam("id") String id, Model model) {
        model.addAttribute("Data", productService.getEveryProductInfo(Long.valueOf(id)));
        return "productmanage/product";
    }

    @GetMapping("/getReview")
    @ResponseBody
    public String getReview(@RequestParam("id") String id, Model model) {
        Map<String,Object> map= new HashMap<>();
        map.put("order_list_id",Long.valueOf(id));
        List<Review> reviews = reviewMapper.selectByMap(map);
        return reviews.isEmpty()?"该订单没有评价！":reviews.get(0).getContent();
    }
}
